/*
* @Author: majingyu
* @Date:   2017-05-28 21:17:25
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-11 19:57:26
*/
import Fetch from 'common/fetch';
//提交用户评论
const submitCommonteType = (type, data) => {
    let action = {
        type:type
    }
    if(type == SUBMIT_COMMONT_SUCCESS){
        action['data'] = data;
    }else if(type == SUBMIT_COMMONT_ERROR){
        action['e'] = data;
    }else if(type == SUBMIT_COMMONT_BIGIN){
        action['data'] = data;
    }
    return action;
}
const SUBMIT_COMMONT_BIGIN = 'SUBMIT_COMMONT_BIGIN';
const SUBMIT_COMMONT_SUCCESS = 'SUBMIT_COMMONT_SUCCESS';
const SUBMIT_COMMONT_ERROR = 'SUBMIT_COMMONT_ERROR';

export const submitCommont = (params) => (dispatch, getState) => {
    dispatch(submitCommonteType(SUBMIT_COMMONT_BIGIN, '正在提交...'));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/web/submit/commont',{
        method:'POST',
         body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(submitCommonteType(SUBMIT_COMMONT_SUCCESS,'提交评论'));
        }else{
            message.error(data.err)
            dispatch(submitCommonteType(SUBMIT_COMMONT_ERROR,'提交评论'));
        }
    }).catch(e=>{
        dispatch(submitCommonteType(SUBMIT_COMMONT_ERROR,'提交评论'));
    })
}

//获取摸个订单的评论列表
const getCommontListType = (type, data) => {
    let action = {
        type:type
    }
    if(type == GET_COMMENT_SUCCESS){
        action['data'] = data;
    }else if(type == GET_COMMENT_ERROR){
        action['e'] = data;
    }else if(type == GET_COMMENT_BIGIN){
        action['data'] = data;
    }
    return action;
}
const GET_COMMENT_BIGIN = 'GET_COMMENT_BIGIN';
const GET_COMMENT_SUCCESS = 'GET_COMMENT_SUCCESS';
const GET_COMMENT_ERROR = 'GET_COMMENT_ERROR';

export const getCommontList = (params) => (dispatch, getState) => {
    dispatch(getCommontListType(GET_COMMENT_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/web/sevices/commontList',{
        method:'POST',
         body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getCommontListType(GET_COMMENT_SUCCESS, data.data));
        }else{
            message.error(data.err)
            dispatch(getCommontListType(GET_COMMENT_ERROR,data.error));
        }
    }).catch(e=>{
        dispatch(getCommontListType(GET_COMMENT_ERROR,e));
    })
}

//修改获取评论列表的订单ID
const CHANGE_COMMENT_ID = 'CHANGE_COMMENT_ID';
export const changeCommentId = (id) => {
    return {
        type:CHANGE_COMMENT_ID,
        id
    }
}

//修改我要评论的订单ID
const EDIT_COMMONT_ORDER_ID = 'EDIT_COMMONT_ORDER_ID';
export const editOrderToCommont = (id) => {
    return {
        type:EDIT_COMMONT_ORDER_ID,
        id
    }
}